<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="input" name="instance"/>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>
	
	<p:choose href="#session">
		<p:when test="/session/groups/@current = 'gr_0'">
			<p:processor name="oxf:unsafe-xslt">
				<p:input name="data" href="#session"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query xsl:version="2.0" collection="/db/portal">
						<xsl:text>xquery version "1.0";</xsl:text>
						<json>{for $d in /users/user[@id = '<xsl:value-of select="/session/username"/>']/interface/widget order by number($d/position/text())*(-1) return <object>
								<name>{string($d/@id)}</name>
								<column>{$d/column/text()}</column>
								<minimized>{$d/minimized/text()}</minimized>
							</object>}</json>
					</xdb:query>
				</p:input>
			</p:processor>
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:unsafe-xslt">
				<p:input name="data" href="#session"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query xsl:version="2.0" collection="/db/portal">
						<xsl:text>xquery version "1.0";</xsl:text>
						<json>{for $d in /groups/group[@id = '<xsl:value-of select="/session/groups/@current"/>']/interface/widget order by number($d/position/text())*(-1) return <object>
								<name>{string($d/@id)}</name>
								<column>{$d/column/text()}</column>
								<minimized>{$d/minimized/text()}</minimized>
							</object>}</json>
					</xdb:query>
				</p:input>
			</p:processor>
		</p:otherwise>
	</p:choose>


	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="interface"/>
	</p:processor>

	<p:processor name="oxf:pipeline">
		<p:input name="data" href="#interface"/>
		<p:input name="config" href="utils/json.xpl"/>
	</p:processor>
</p:config>